package com.maple.myskin.core.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

/**
 * Created by maple on 2020-07-30 09:47
 * E-Mail Address：740917401@qq.com
 */
public class SkinResources {
    private volatile static SkinResources instance;

    private Resources mSkinResources;
    private String mSkinPkgName;
    private boolean isDefaultSkin = true;

    private Resources mAppResources;

    private SkinResources(Context context) {
        mAppResources = context.getResources();
    }

    public static void init(Context context) {
        if (instance == null) {
            synchronized (SkinResources.class) {
                if (instance == null) {
                    instance = new SkinResources(context);
                }
            }
        }
    }

    public static SkinResources getInstance() {
        return instance;
    }

    /**
     * 重置
     */
    public void reset() {
        mSkinResources = null;
        mSkinPkgName = "";
        isDefaultSkin = true;
    }

    /**
     * 应用皮肤
     *
     * @param resources 皮肤包资源
     * @param pkgName   皮肤包包名
     */
    public void applySkin(Resources resources, String pkgName) {
        mSkinResources = resources;
        mSkinPkgName = pkgName;
        isDefaultSkin = TextUtils.isEmpty(pkgName) || resources == null;//资源或包名为空就用默认皮肤
    }

    /**
     * 获取皮肤包的资源id
     *
     * @param appResId app的资源id
     * @return 皮肤包的资源id 如果没有对应资源id 则返回0
     */
    public int getSkinResId(int appResId) {

        if (isDefaultSkin) {
            return appResId;
        }

        //在皮肤包中的资源id  可能跟当前程序的 资源id 不一致
        //获取app的资源id对应的资源名跟类型，然后去皮肤包找到对应的资源id
        String resName = mAppResources.getResourceEntryName(appResId);
        String resType = mAppResources.getResourceTypeName(appResId);
        int skinResId = mSkinResources.getIdentifier(resName, resType, mSkinPkgName);
        return skinResId;
    }

    /**
     * 获取皮肤包中对应资源的颜色
     *
     * @param appResId app的资源id
     * @return 皮肤包中对应资源的颜色 如果没有则使用默认颜色
     */
    public int getColor(int appResId) {
        if (isDefaultSkin) {
            return mAppResources.getColor(appResId);
        }

        int skinResId = getSkinResId(appResId);
        if (skinResId == 0) {
            return mAppResources.getColor(appResId);
        }
        return mSkinResources.getColor(skinResId);
    }

    /**
     * 获取皮肤包中对应资源的颜色状态列表（selector）
     *
     * @param appResId app的资源id
     * @return 皮肤包中对应资源的颜色状态列表 如果没有则使用默认颜色状态列表
     */
    public ColorStateList getColorStateList(int appResId) {
        if (isDefaultSkin) {
            return mAppResources.getColorStateList(appResId);
        }
        int skinResId = getSkinResId(appResId);
        if (skinResId == 0) {
            return mAppResources.getColorStateList(appResId);
        }
        return mSkinResources.getColorStateList(skinResId);
    }

    /**
     * 获取皮肤包中对应资源的Drawable
     *
     * @param appResId app的资源id
     * @return 皮肤包中对应资源的Drawable 如果没有则使用默认Drawable
     */
    public Drawable getDrawable(int appResId) {
        if (isDefaultSkin) {
            return mAppResources.getDrawable(appResId);
        }
        int skinResId = getSkinResId(appResId);
        if (skinResId == 0) {
            return mAppResources.getDrawable(appResId);
        }
        return mSkinResources.getDrawable(skinResId);
    }

    /**
     * 获取皮肤包中对应资源的背景
     *
     * @param appResId app的资源id
     * @return 皮肤包中对应资源的背景 可能是color可能是Drawable
     */
    public Object getBackground(int appResId) {
        String resourceTypeName = mAppResources.getResourceTypeName(appResId);

        if (resourceTypeName.equals("color")) {
            return getColor(appResId);
        } else {
            return getDrawable(appResId);
        }
    }

    /**
     * 获取皮肤包中对应资源的字符串
     *
     * @param appResId app的资源id
     * @return 皮肤包中对应资源的字符串
     */
    public String getString(int appResId) {
        try {
            if (isDefaultSkin) {
                return mAppResources.getString(appResId);
            }
            int skinResId = getSkinResId(appResId);
            if (skinResId == 0) {
                return mAppResources.getString(appResId);
            }
            return mSkinResources.getString(skinResId);
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取皮肤包中对应资源的字体
     *
     * @param appResId app的资源id
     * @return 皮肤包中对应资源的字体
     */
    public Typeface getTypeface(int appResId) {
        String skinTypefacePath = getString(appResId);//获取皮肤包中 自定义string属性 skinTypeface 对应的值
        if (TextUtils.isEmpty(skinTypefacePath)) {
            return Typeface.DEFAULT;
        }
        try {
            if (isDefaultSkin) {
                return Typeface.createFromAsset(mAppResources.getAssets(), skinTypefacePath);
            }
            return Typeface.createFromAsset(mSkinResources.getAssets(), skinTypefacePath);
        } catch (RuntimeException e) {
        }
        return Typeface.DEFAULT;
    }
}
